package com.ruoyi.dataserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.dataserver.api.dto.LengthResultDTO;
import com.ruoyi.dataserver.api.dto.ScopeDto;
import com.ruoyi.dataserver.api.dto.StatisticDto;
import com.ruoyi.dataserver.api.dto.StatisticResultDTO;
import com.ruoyi.dataserver.config.ScConfigAddress;
import com.ruoyi.dataserver.service.IStatisticsService;
import com.ruoyi.dataserver.utils.CommonConstants;
import com.ruoyi.dataserver.utils.R;
import com.ruoyi.dataserver.utils.ExcelExportUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @ProjectName intelligent-city
 * @Package com.lab.dataserver.controller
 * @ClassName StatisticController
 * @Description 区域统计-管线相关
 * @Company LAB
 * @Author LXK
 * @Date 2020/6/19 9:39
 * @UpdateUser
 * @UpdateDate
 * @UpdateRemark
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/statistic")
@Slf4j
public class LineStatisticController {

    @Autowired
    private IStatisticsService statisticsService;


    @Autowired
    private ScConfigAddress scConfigAddress;

    /**
     * @param scopeDto :
     * @return java.lang.String
     * @Title pipelineStatistics
     * @Description 画框统计-管线长度统计
     * @author heff
     * @date 2024/1/10 13:41
     * @UpdateUser
     * @UpdateDate
     * @UpdateRemark
     */
    @PostMapping("/lengthStatistics")
    public R lengthStatistics(@RequestBody ScopeDto scopeDto, HttpServletResponse response) {
        JSONObject obj = new JSONObject();
        LengthResultDTO wsszdto =  statisticsService.queryLengthResultBySql("WSGSZ_LINE", scopeDto);
        obj.put("市政污水管线",wsszdto.getLength());
        LengthResultDTO wsxqdto =  statisticsService.queryLengthResultBySql("WSGXQ_LINE", scopeDto);
        obj.put("小区污水管线",wsxqdto.getLength());
        LengthResultDTO ysszdto =  statisticsService.queryLengthResultBySql("YSGSZ_LINE", scopeDto);
        obj.put("市政雨水管线",ysszdto.getLength());
        LengthResultDTO ysxqdto =  statisticsService.queryLengthResultBySql("YSGXQ_LINE", scopeDto);
        obj.put("小区雨水管线",ysxqdto.getLength());
        return R.ok(obj,"管线长度统计结果");
    }

    /**
     * @param scopeDto :
     * @return java.lang.String
     * @Title pipelineStatistics
     * @Description 画框统计-管线长度统计
     * @author heff
     * @date 2024/1/10 13:41
     * @UpdateUser
     * @UpdateDate
     * @UpdateRemark
     */
    @PostMapping("/lengthStatisticsNw")
    public R lengthStatisticsnw(@RequestBody ScopeDto scopeDto, HttpServletResponse response) {
        JSONObject obj = new JSONObject();
        LengthResultDTO wsszdto =  statisticsService.queryLengthResultBySql("WSGSZ_LINE", scopeDto);
        obj.put("市政污水管线",wsszdto.getLength());
        LengthResultDTO wsxqdto =  statisticsService.queryLengthResultBySql("WSGXQ_LINE", scopeDto);
        obj.put("小区污水管线",wsxqdto.getLength());
        LengthResultDTO ysszdto =  statisticsService.queryLengthResultBySql("YSGSZ_LINE", scopeDto);
        obj.put("市政雨水管线",ysszdto.getLength());
        LengthResultDTO ysxqdto =  statisticsService.queryLengthResultBySql("YSGXQ_LINE", scopeDto);
        obj.put("小区雨水管线",ysxqdto.getLength());
        LengthResultDTO qtdto =  statisticsService.queryLengthResultBySql("BMG_LINE", scopeDto);
        obj.put("其他管线",qtdto.getLength());
        LengthResultDTO dldto =  statisticsService.queryLengthResultBySql("GDX_LINE", scopeDto);
        obj.put("电力管线",dldto.getLength());
        LengthResultDTO jsdto =  statisticsService.queryLengthResultBySql("JSG_LINE", scopeDto);
        obj.put("给水管线",jsdto.getLength());
        LengthResultDTO rldto =  statisticsService.queryLengthResultBySql("RSG_LINE", scopeDto);
        obj.put("热力管线",rldto.getLength());
        LengthResultDTO trqdto =  statisticsService.queryLengthResultBySql("TRQ_LINE", scopeDto);
        obj.put("燃气管线",trqdto.getLength());
        LengthResultDTO xxdto =  statisticsService.queryLengthResultBySql("XXX_LINE", scopeDto);
        obj.put("信息管线",xxdto.getLength());
        LengthResultDTO gydto =  statisticsService.queryLengthResultBySql("YLG_LINE", scopeDto);
        obj.put("工业管线",gydto.getLength());
        return R.ok(obj,"管线长度统计结果");
    }



    /**
     * @param statisticDto :
     * @return java.lang.String
     * @Title pipelineStatistics
     * @Description 区域统计-管线长度统计
     * @author heff
     * @date 2020/7/8 13:41
     * @UpdateUser
     * @UpdateDate
     * @UpdateRemark
     */
    @PostMapping("/pipeline")
    public R pipelineStatistics(@RequestBody StatisticDto statisticDto, HttpServletResponse response) throws IOException {

        if (statisticDto != null) {
            StatisticResultDTO statisticResultDTO = statisticsService.pipeLines(statisticDto);
            if(statisticDto.getExport()!=null&&statisticDto.getExport().equals(CommonConstants.EXPORT)) {
                String deviceTemplate = scConfigAddress.getExcelExportFilePath();
                ExcelExportUtils excelExportUtils = new ExcelExportUtils();
                excelExportUtils.lengthUtil(deviceTemplate,statisticResultDTO, response);
            }
            return R.ok(statisticResultDTO, "管线长度统计结果");

        }

        return R.ok("未查询到结果");
    }

    /**
     * @param statisticDto :
     * @return com.lab.dataserver.utils.R
     * @Title pipeDiameterStatistics
     * @Description 管径分类统计
     * @author heff
     * @date 2020/7/13 13:41
     * @UpdateUser
     * @UpdateDate
     * @UpdateRemark
     */
    @PostMapping("/pipediameter")
    public R pipeDiameterStatistics(@RequestBody StatisticDto statisticDto, HttpServletResponse response) throws IOException {
        if (statisticDto != null) {
            StatisticResultDTO statisticResultDTO = statisticsService.pipeDiameter(statisticDto);
            if(statisticDto.getExport()!=null&&statisticDto.getExport().equals(CommonConstants.EXPORT)) {
                String deviceTemplate = scConfigAddress.getExcelExportFilePath();
                ExcelExportUtils excelExportUtils = new ExcelExportUtils();
                excelExportUtils.lengthUtil(deviceTemplate,statisticResultDTO, response);
            }
            return R.ok(statisticResultDTO, "管径统计结果");
        }

        return R.ok("未查询到结果");
    }
}